<template>
	<div>
		<el-card class="!border-none mb-4" shadow="never">
			<el-form class="mb-[-16px]" :model="queryParams" inline>
				<el-form-item label="分组名称" prop="name">
					<el-input class="w-[280px]" v-model="queryParams.name" clearable placeholder="请输入分组名称" />
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="resetPage">查询</el-button>
					<el-button @click="resetParams">重置</el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card class="!border-none" v-loading="pager.loading" shadow="never">
			<el-button v-perms="['user.user_group/add']" type="primary" @click="handleAdd">
				<template #icon>
					<icon name="el-icon-Plus" />
				</template>
				新增
			</el-button>
			<el-button v-perms="['user.user_group/delete']" :disabled="!selectData.length"
				@click="handleDelete(selectData)">
				删除
			</el-button>
			<div class="mt-4">
				<el-table :data="pager.lists" @selection-change="handleSelectionChange">
					<el-table-column type="selection" width="55" />
					<el-table-column label="分组名称" prop="name" show-overflow-tooltip />
					<el-table-column label="24小时订单数" prop="rule_num" show-overflow-tooltip />
					<el-table-column label="用户数量" prop="user_num" show-overflow-tooltip />
					<el-table-column label="操作" width="160" fixed="right">
						<template #default="{ row }">
							<div class="flex justify-between">
								<el-button v-perms="['user.user_group_rule/lists']" type="primary" link
									@click="handleRule(row.id)">
									规则
								</el-button>
								<el-button v-perms="['user.user_group/edit']" type="primary" link @click="handleEdit(row)">
									编辑
								</el-button>
								<el-button v-perms="['user.user_group/delete']" type="danger" link
									@click="handleDelete(row.id)">
									删除
								</el-button>
							</div>
							
						</template>
					</el-table-column>
				</el-table>
			</div>
			<div class="flex mt-4 justify-end">
				<pagination v-model="pager" @change="getLists" />
			</div>
		</el-card>
		<edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
		<rule-popup v-if="showRule" ref="ruleRef" @close="showRule = false" />
	</div>
</template>

<script lang="ts" setup name="userGroupLists">
	import { usePaging } from '@/hooks/usePaging'
	import { apiUserGroupLists, apiUserGroupDelete } from '@/api/user_group'
	import feedback from '@/utils/feedback'
	import EditPopup from './edit.vue'
	import RulePopup from './rule/index.vue'

	const editRef = shallowRef<InstanceType<typeof EditPopup>>()
	const ruleRef = shallowRef<InstanceType<typeof RulePopup>>()
	// 是否显示编辑框
	const showEdit = ref(false)
	const showRule = ref(false)

	// 查询条件
	const queryParams = reactive({
		name: ''
	})

	// 选中数据
	const selectData = ref<any[]>([])

	// 表格选择后回调事件
	const handleSelectionChange = (val : any[]) => {
		selectData.value = val.map(({ id }) => id)
	}

	// 分页相关
	const { pager, getLists, resetParams, resetPage } = usePaging({
		fetchFun: apiUserGroupLists,
		params: queryParams
	})

	const handleRule = async (id : number) => {
		showRule.value = true
		await nextTick()
		ruleRef.value?.open()
		ruleRef.value?.setFormData({ group_id: id })
	}

	// 添加
	const handleAdd = async () => {
		showEdit.value = true
		await nextTick()
		editRef.value?.open('add')
	}

	// 编辑
	const handleEdit = async (data : any) => {
		showEdit.value = true
		await nextTick()
		editRef.value?.open('edit')
		editRef.value?.setFormData(data)
	}

	// 删除
	const handleDelete = async (id : number | any[]) => {
		await feedback.confirm('确定要删除？')
		await apiUserGroupDelete({ id })
		getLists()
	}

	getLists()
</script>